// pages/orderjubao/orderjubao.js
var app=getApp();
var c=require('../common.js');
var upimg = require('../upimg.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgLocation:'../img/addimg.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var orderid = options.orderid;
    var user_id=app.u.user_id;
    var sql = 'w(user_id=' + user_id + ')f(phone)l(1)';
    var that = this;
    this.setData({ orderid: orderid})
    c.ajax(
      app.dbs + 'user', {
        sql: sql
      },
      res => {
        that.setData({
          phone: res.data.phone
        });
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  checkboxChange:function(e){
    var cb=e.detail.value;
    this.setData({ chebox: cb});
  },
  textareainfo:function(e){
    var value=e.detail.value;
    this.setData({ textarea: value });
  },
  clickimg:function(){
    var timeinfo = new Date().getTime();
    var path = 'user/' + this.data.phone + timeinfo + '.png';
    var that = this;
    var returninfo = upimg.simpleUpload(
      path,
      function (res) {
        that.setData({
          imgLocation: res[0].imgLocation,
          dbs: res[0].dbs
        });
      }
    );
  },
  inputset:function(e){
    var value = e.detail.value;
    this.setData({ inputinfo: value });
  },
  submit:function(){
    wx.showLoading({
      title: '上传中...',
    })
    var chebox = this.data.chebox;
    var textarea = this.data.textarea;
    var inputinfo = this.data.inputinfo;
    var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
    if (!phoneReg.test(inputinfo)) {
      wx.showToast({
        title: '手机号码有误',
        icon:'none'
      })
      return false;
    }

    var dbs = this.data.dbs;
    var orderid = this.data.orderid;
    var uid = app.u.user_id;
    c.ajax(
      app.url +'reportAdd',
      { chebox: chebox, textarea: textarea, inputinfo: inputinfo, dbs: dbs, orderid: orderid, uid: uid},
      res=>{
        wx.hideLoading();
        console.log(res.data);
        if (res.data){
             wx.showToast({
               title: '上传成功',
               icon:'none'
             })
             setTimeout(function(){
                wx.navigateTo({
                  url: '/pages/myorderguanli/myorderguanli',
                })
             },1000)
        }
      }
    )
  }
})